package com.smarty.net;

import com.smarty.net.socket.WorkSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;

@Component
@Slf4j
public class AppRun implements ApplicationRunner {
    @Resource
    private Executor executor;
    @Override
    public void run(ApplicationArguments args) throws Exception {
//        AtomicBoolean isAccept= new AtomicBoolean(false);
        try(ServerSocket serverSocket=new ServerSocket(18080)){
            while (true){
                Socket accept = serverSocket.accept();
                log.info("接收一个链接");
//                isAccept.set(false);
                executor.execute(()->{
                    try {
//                        isAccept.set(true);
                        WorkSocket workSocket=new WorkSocket();
                        workSocket.start(accept);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
                Thread.sleep(3000);
                log.info("接收一个链接-finish");
//                while (isAccept.get() ==false){
//                    Thread.sleep(1l);
//                }
            }

        }
    }
}
